<template>
  <div class="app-container">
    <div class="Search">
      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="query">
          <el-form-item v-has="'PLATFROM_TENANT_QUERY'">
            <el-cascader ref="tenantTree" placeholder="所属租户" :props="tenantProps" :options="tenantOptions"
              @change="selectTenant" clearable collapse-tags filterable></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-cascader ref="organTree" placeholder="所属组织" :props="props" :options="organOptions" @change="selectOrgan"
              collapse-tags clearable filterable></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-select v-model="query.params.chargeType" clearable placeholder="计量方式">
              <el-option v-for="item in query.chargeTypeOptions" :key="item.value" :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.logisticsCode" placeholder="物流商编号" />
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.logisticsName" placeholder="物流商名称" />
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.name" placeholder="运费模板名称" />
          </el-form-item>
          <el-form-item>
            <el-select v-model="query.params.serviceType" clearable placeholder="服务类型">
              <el-option v-for="item in query.serviceTypeOptions" :key="item.value" :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="query.params.status" placeholder="状态">
              <el-option v-for="item in query.statusOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-select v-model="query.params.type" clearable placeholder="模板类型，1.默认模板，2.自定义模板">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </div>

    <div class="block">
      <el-table v-loading="listLoading" :data="list"
        element-loading-text="Loading" border fit highlight-current-row>
        <el-table-column align="center" label="序号" width="95">
          <template slot-scope="scope">
            {{ scope.$index }}
          </template>
        </el-table-column>
        <el-table-column label="运费模板名称" width="110">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column label="计量方式" width="110">
          <template slot-scope="scope">
            {{ scope.row.chargeType }}
          </template>
        </el-table-column>
        <el-table-column label="物流商编号" width="110">
          <template slot-scope="scope">
            {{ scope.row.logisticsCode }}
          </template>
        </el-table-column>
        <el-table-column label="物流商名称" width="110">
          <template slot-scope="scope">
            {{ scope.row.logisticsName }}
          </template>
        </el-table-column>

        <el-table-column label="数量首费" width="110">
          <template slot-scope="scope">
            {{ scope.row.fristNumPrice }}
          </template>
        </el-table-column>
        <el-table-column label="体积首费" width="110">
          <template slot-scope="scope">
            {{ scope.row.fristVolumePrice }}
          </template>
        </el-table-column>
        <el-table-column label="重要首费" width="110">
          <template slot-scope="scope">
            {{ scope.row.fristWeightPrice }}
          </template>
        </el-table-column>
        <el-table-column label="数量续费" width="110">
          <template slot-scope="scope">
            {{ scope.row.nextNumPrice }}
          </template>
        </el-table-column>
        <el-table-column label="体积续费" width="110">
          <template slot-scope="scope">
            {{ scope.row.nextVolumePrice }}
          </template>
        </el-table-column>
        <el-table-column label="重要续费" width="110">
          <template slot-scope="scope">
            {{ scope.row.nextWeightPrice }}
          </template>
        </el-table-column>
        <el-table-column label="最多数量" width="110">
          <template slot-scope="scope">
            {{ scope.row.numMax }}
          </template>
        </el-table-column>
        <el-table-column label="最少数量" width="110">
          <template slot-scope="scope">
            {{ scope.row.numMin }}
          </template>
        </el-table-column>
        <el-table-column label="组织构构" width="110">
          <template slot-scope="scope">
            {{ scope.row.organId }}
          </template>
        </el-table-column>
        <el-table-column label="组织路径" width="110">
          <template slot-scope="scope">
            {{ scope.row.organPath }}
          </template>
        </el-table-column>


        <el-table-column label="服务类型：1:经济，2快速" width="110">
          <template slot-scope="scope">
            {{ scope.row.serviceType }}
          </template>
        </el-table-column>
        <el-table-column label="排序" width="110">
          <template slot-scope="scope">
            {{ scope.row.sorted }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="110">
          <template slot-scope="scope">
            {{ scope.row.status }}
          </template>
        </el-table-column>
        <el-table-column label="所属租户" width="110">
          <template slot-scope="scope">
            {{ scope.row.tenantId }}
          </template>
        </el-table-column>
        <el-table-column label="模板类型，1.默认模板，2.自定义模板" width="110">
          <template slot-scope="scope">
            {{ scope.row.type }}
          </template>
        </el-table-column>

        <el-table-column label="最大体积" width="110">
          <template slot-scope="scope">
            {{ scope.row.volumeMax }}
          </template>
        </el-table-column>
        <el-table-column label="最小体积" width="110">
          <template slot-scope="scope">
            {{ scope.row.volumeMin }}
          </template>
        </el-table-column>
        <el-table-column label="最大重量" width="110">
          <template slot-scope="scope">
            {{ scope.row.weightMax }}
          </template>
        </el-table-column>
        <el-table-column label="最小重量" width="110">
          <template slot-scope="scope">
            {{ scope.row.weightMin }}
          </template>
        </el-table-column>

        <el-table-column label="生成者" width="110">
          <template slot-scope="scope">
            {{ scope.row.createBy }}
          </template>
        </el-table-column>
        <el-table-column label="生成日期" width="110">
          <template slot-scope="scope">
            {{ scope.row.createDate }}
          </template>
        </el-table-column>
        <el-table-column label="更新者" width="110">
          <template slot-scope="scope">
            {{ scope.row.updateBy }}
          </template>
        </el-table-column>
        <el-table-column label="更新日期" width="110">
          <template slot-scope="scope">
            {{ scope.row.updateDate }}
          </template>
        </el-table-column>
        <el-table-column label="备注" width="110">
          <template slot-scope="scope">
            {{ scope.row.remark }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" align="center" prop="created_at" label="操作" width="200">
          <template slot-scope="scope">
            <el-link type="warning" icon="el-icon-edit" size="mini"
              @click="handleEdit(scope.$index, scope.row)">修改</el-link>
            <el-link type="danger" icon="el-icon-delete" size="mini"
              @click="handleDelete(scope.$index, scope.row)">删除</el-link>
            <el-link type="primary" icon="el-icon-search" size="mini"
              @click="handleShowFreightTemplate(scope.row)">查看包邮地区</el-link>
          </template>
        </el-table-column>
      </el-table>

    </div>
    <div class="block">
      <el-pagination :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>


    <div class="block">
      <el-dialog title="包邮地区" width="70%" :visible.sync="dialogFreightTemplateAreaListVisible">
        <el-table v-loading="listLoading" :data="freightTemplateAreaList" element-loading-text="Loading" border fit
          highlight-current-row>
          <el-table-column align="center" label="序号" width="45">
            <template slot-scope="scope">
              {{ scope.$index }}
            </template>
          </el-table-column>
          <el-table-column label="区县编号" width="110">
            <template slot-scope="scope">
              {{ scope.row.areaId }}
            </template>
          </el-table-column>
          <el-table-column label="区县" width="110">
            <template slot-scope="scope">
              {{ scope.row.area }}
            </template>
          </el-table-column>
          <el-table-column label="城市编号" width="110">
            <template slot-scope="scope">
              {{ scope.row.cityId }}
            </template>
          </el-table-column>
          <el-table-column label="城市" width="110">
            <template slot-scope="scope">
              {{ scope.row.city }}
            </template>
          </el-table-column>
          <el-table-column label="省份编号" width="110">
            <template slot-scope="scope">
              {{ scope.row.privinceId }}
            </template>
          </el-table-column>
          <el-table-column label="省份" width="110">
            <template slot-scope="scope">
              {{ scope.row.privince }}
            </template>
          </el-table-column>
          <el-table-column label="所属组织" width="110">
            <template slot-scope="scope">
              {{ scope.row.organId }}
            </template>
          </el-table-column>
          <el-table-column label="模板编号" width="110">
            <template slot-scope="scope">
              {{ scope.row.templateId }}
            </template>
          </el-table-column>
          <el-table-column label="备注" width="110">
            <template slot-scope="scope">
              {{ scope.row.remark }}
            </template>
          </el-table-column>
          <el-table-column label="所属租户" width="110">
            <template slot-scope="scope">
              {{ scope.row.tenantId }}
            </template>
          </el-table-column>
        </el-table>

        <div class="block">
          <el-pagination :current-page="query.freightTemplateArea.currentPage" :page-sizes="[10, 20, 30, 40]"
            :page-size="query.freightTemplateArea.pageSize" layout="total, sizes, prev, pager, next, jumper"
            :total="query.freightTemplateArea.total" @size-change="handleFreightTemplateAreaSizeChange"
            @current-change="handleFreightTemplateAreaCurrentChange" />
        </div>
      </el-dialog>
    </div>




    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible">
        <el-form :model="form" :rules="FreightTemplateRules" ref="FreightTemplateForm">
          <el-form-item label="计量方式：1.按重量，2.按数量" required prop="chargeType">
            <el-select v-model="form.chargeType" placeholder="计量方式：1.按重量，2.按数量">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="编号" required prop="id">
            <el-input v-model="form.id" placeholder="编号" auto-complete="off" />
          </el-form-item>
          <el-form-item label="物流商编号" required prop="logisticsCode">
            <el-input v-model="form.logisticsCode" placeholder="物流商编号" auto-complete="off" />
          </el-form-item>
          <el-form-item label="物流商名称" required prop="logisticsName">
            <el-input v-model="form.logisticsName" placeholder="物流商名称" auto-complete="off" />
          </el-form-item>
          <el-form-item label="运费模板名称" required prop="name">
            <el-input v-model="form.name" placeholder="运费模板名称" auto-complete="off" />
          </el-form-item>
          <el-form-item label="最多数量" required prop="numMax">
            <el-select v-model="form.numMax" placeholder="最多数量">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="最少数量" required prop="numMin">
            <el-select v-model="form.numMin" placeholder="最少数量">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="组织构构" required prop="organId">
            <el-input v-model="form.organId" placeholder="组织构构" auto-complete="off" />
          </el-form-item>
          <el-form-item label="组织路径" required prop="organPath">
            <el-input v-model="form.organPath" placeholder="组织路径" auto-complete="off" />
          </el-form-item>
          <el-form-item label="父节点编号" required prop="parentId">
            <el-input v-model="form.parentId" placeholder="父节点编号" auto-complete="off" />
          </el-form-item>

          <el-form-item label="服务类型：1:经济，2快速" required prop="serviceType">
            <el-select v-model="form.serviceType" placeholder="服务类型：1:经济，2快速">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="排序" required prop="sorted">
            <el-select v-model="form.sorted" placeholder="排序">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" required prop="status">
            <el-select v-model="form.status" placeholder="状态">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属租户" required prop="tenantId">
            <el-input v-model="form.tenantId" placeholder="所属租户" auto-complete="off" />
          </el-form-item>
          <el-form-item label="模板类型，1.默认模板，2.自定义模板" required prop="type">
            <el-select v-model="form.type" placeholder="模板类型，1.默认模板，2.自定义模板">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="最大体积" required prop="volumeMax">
            <el-select v-model="form.volumeMax" placeholder="最大体积">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="最小体积" required prop="volumeMin">
            <el-select v-model="form.volumeMin" placeholder="最小体积">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="最大重量" required prop="weightMax">
            <el-select v-model="form.weightMax" placeholder="最大重量">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="最小重量" required prop="weightMin">
            <el-select v-model="form.weightMin" placeholder="最小重量">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注" required prop="remark">
            <el-input v-model="form.remark" placeholder="备注" auto-complete="off" />
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>


    <div class="block">
      <el-drawer title="新增运费模板" size="55%" :visible.sync="drawerFreightTemplateVisible" :direction="direction"
        :before-close="handleClose">
        <div class="app-container">
          <el-form :model="form" :rules="FreightTemplateRules" ref="FreightTemplateForm">
            <el-form-item label="运费模板名称" required prop="name">
              <el-input v-model="form.name" placeholder="运费模板名称" auto-complete="off" />
            </el-form-item>
            <el-form-item label="物流商名称" required prop="logisticsName">
              <el-input v-model="form.logisticsName" placeholder="物流商名称" @focus="handleShowLogistics"
                suffix-icon="el-icon-truck" auto-complete="off" />
            </el-form-item>
            <el-form-item label="物流商编号" required prop="logisticsCode">
              <el-input v-model="form.logisticsCode" placeholder="物流商编号" disabled auto-complete="off" />
            </el-form-item>
            <el-form-item label="是否包邮" required prop="chargeType">
              <el-radio-group disabled v-model="form.isFeeLimit">
                <el-radio label="0">包邮</el-radio>
                <el-radio label="1">自定义运费</el-radio>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="计价方式" required prop="chargeType">
              <el-radio-group v-model="form.chargeType">
                <el-radio v-for="chargeType in query.chargeTypeOptions" :label="chargeType.value">{{ chargeType.label
                }}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="服务类型" required prop="serviceType">
              <el-radio-group v-model="form.serviceType">
                <el-radio v-for="serviceType in query.serviceTypeOptions" :label="serviceType.value">{{ serviceType.label
                }}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item v-if="form.chargeType == 1" label="最小重量" required prop="weightMin">
              <el-input placeholder="最小重量" v-model="form.weightMin" />
            </el-form-item>
            <el-form-item v-if="form.chargeType == 1" label="最大重量" required prop="weightMax">
              <el-input placeholder="最大重量" v-model="form.weightMax" />
            </el-form-item>
            <el-form-item v-if="form.chargeType == 1" label="重量首费" required prop="fristWeightPrice">
              <el-input placeholder="重量首费" v-model="form.fristWeightPrice" />
            </el-form-item>

            <el-form-item v-if="form.chargeType == 1" label="重量续费" required prop="nextWeightPrice">
              <el-input placeholder="重量续费" v-model="form.nextWeightPrice" />
            </el-form-item>


            <el-form-item v-if="form.chargeType == 2" label="最小数量" required prop="numMin">
              <el-input placeholder="最小数量" v-model="form.numMin" />
            </el-form-item>
            <el-form-item v-if="form.chargeType == 2" label="最大数量" required prop="numMax">
              <el-input placeholder="最大数量" v-model="form.numMax" />
            </el-form-item>

            <el-form-item v-if="form.chargeType == 2" label="数量首费" required prop="fristNumPrice">
              <el-input placeholder="数量首费" v-model="form.fristNumPrice" />
            </el-form-item>

            <el-form-item v-if="form.chargeType == 2" label="数量续费" required prop="nextNumPrice">
              <el-input placeholder="数量续费" v-model="form.nextNumPrice" />
            </el-form-item>


            <el-form-item v-if="form.chargeType == 3" label="最小体积" required prop="volumeMin">
              <el-input placeholder="最小体积" v-model="form.volumeMin" />
            </el-form-item>
            <el-form-item v-if="form.chargeType == 3" label="最大体积" required prop="volumeMax">
              <el-input placeholder="最大体积" v-model="form.volumeMax" />
            </el-form-item>

            <el-form-item v-if="form.chargeType == 3" label="体积首费" required prop="fristVolumePrice">
              <el-input placeholder="体积首费" v-model="form.fristVolumePrice" />
            </el-form-item>

            <el-form-item v-if="form.chargeType == 3" label="体积续费" required prop="nextVolumePrice">
              <el-input placeholder="体积续费" v-model="form.nextVolumePrice" />
            </el-form-item>

            <el-form-item label="是否启用" required prop="status">
              <el-tooltip :content="'是否前台展示: ' + (scope.row.show == 1) ? '是' : '否'" placement="top">
                <el-switch v-model="form.status" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
                  :inactive-value="0">
                </el-switch>
              </el-tooltip>
            </el-form-item>


            <el-card class="box-card" style="margin: 10px 0 0 0;" shadow="hover">
              <div slot="header" class="clearfix">
                <el-button type="primary" style="float: left;" icon="el-icon-share" plain>设置地区</el-button>
              </div>
              <div class="text item">
                <el-tree ref="regionTree" :data="administrativeRegionTree" show-checkbox node-key="id"
                  :props="administrativeRegionProps">
                </el-tree>
              </div>
            </el-card>
          </el-form>

          <div slot="footer" style="margin: 10px 10px 10px 0; " class="dialog-footer">
            <el-button @click="drawerFreightTemplateVisible = false">取 消</el-button>
            <el-button type="primary" @click="handleCreateFreightTemplate">确 定</el-button>
          </div>
        </div>
      </el-drawer>
    </div>

    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogLogisticsListVisible">
        <el-table v-loading="listLoading" :data="logisticsList" element-loading-text="Loading" border fit
          highlight-current-row>

          <el-table-column align="center" label="选择" width="35">
            <template slot-scope="scope">
              <el-radio :label="scope.row.id" v-model="logistics.id"
                @change="handelChooseLogistics(scope.row)">&nbsp;&nbsp;</el-radio>
            </template>
          </el-table-column>

          <el-table-column label="承运商名称" width="110">
            <template slot-scope="scope">
              {{ scope.row.name }}
            </template>
          </el-table-column>
          <el-table-column label="承运商代码" width="110">
            <template slot-scope="scope">
              {{ scope.row.code }}
            </template>
          </el-table-column>
          <el-table-column label="承运商图标" width="110">
            <template slot-scope="scope">
              <el-image style="width: 100px; height: 100px" :src="scope.row.icon">
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="是否可以自定义运费" width="110">
            <template slot-scope="scope">
              <!-- <el-tag type="success">{{ scope.row.customFeeLimit?'是':'否' }}</el-tag> -->
              <el-tooltip :content="'是否可以自定义运费: ' + scope.row.customFeeLimit ? '是' : '否'" placement="top">
                <el-switch v-model="scope.row.customFeeLimit" active-color="#13ce66" inactive-color="#ff4949"
                  :active-value="1" @change="handleSwitchUpdate($event, scope.$index, scope.row, 'customFeeLimit')"
                  :inactive-value="0">
                </el-switch>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="是否可以自定义送达时间" width="110">
            <template slot-scope="scope">
              <!-- <el-tag type="success">{{ scope.row.customTimeLimit?'是':'否' }}</el-tag> -->
              <el-tooltip :content="'是否可以自定义送达时间: ' + scope.row.customTimeLimit ? '是' : '否'" placement="top">
                <el-switch v-model="scope.row.customTimeLimit" active-color="#13ce66" inactive-color="#ff4949"
                  :active-value="1" @change="handleSwitchUpdate($event, scope.$index, scope.row, 'customTimeLimit')"
                  :inactive-value="0">
                </el-switch>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="是否有运费限制" width="110">
            <template slot-scope="scope">
              <el-tooltip :content="'是否有运费限制: ' + scope.row.freightLimit ? '是' : '否'" placement="top">
                <el-switch v-model="scope.row.freightLimit" active-color="#13ce66" inactive-color="#ff4949"
                  :active-value="1" @change="handleSwitchUpdate($event, scope.$index, scope.row, 'freightLimit')"
                  :inactive-value="0">
                </el-switch>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="是否前台展示" width="110">
            <template slot-scope="scope">
              <!-- <el-tag type="success">{{ scope.row.show?'是':'否' }}</el-tag> -->
              <el-tooltip :content="'是否前台展示: ' + (scope.row.show == 1) ? '是' : '否'" placement="top">
                <el-switch v-model="scope.row.show" active-color="#13ce66" inactive-color="#ff4949"
                  @change="handleSwitchUpdate($event, scope.$index, scope.row, 'show')" :active-value="1"
                  :inactive-value="0">
                </el-switch>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="组织" width="110">
            <template slot-scope="scope">
              {{ scope.row.organId }}
            </template>
          </el-table-column>
          <el-table-column label="组织路径" width="110">
            <template slot-scope="scope">
              {{ scope.row.organPath }}
            </template>
          </el-table-column>
          <el-table-column label="物流查询网址" width="110">
            <template slot-scope="scope">
              {{ scope.row.url }}
            </template>
          </el-table-column>
          <el-table-column label="排序" width="110">
            <template slot-scope="scope">
              {{ scope.row.sorted }}
            </template>
          </el-table-column>
          <el-table-column label="状态" width="110">
            <template slot-scope="scope">
              {{ scope.row.status }}
            </template>
          </el-table-column>
          <el-table-column label="所属租户" width="110">
            <template slot-scope="scope">
              {{ scope.row.tenantId }}
            </template>
          </el-table-column>

          <el-table-column label="备注" width="110">
            <template slot-scope="scope">
              {{ scope.row.remark }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" align="center" prop="created_at" label="操作" width="200">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-search" size="mini"
                @click="handleEdit(scope.$index, scope.row)">修改</el-button>
              <el-button type="danger" icon="el-icon-plus" size="mini"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogLogisticsListVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>

  </div>
</template>

<script>
import { getList, queryForList, doDelete, doRemove, doEdit } from '@/api/FreightTemplate/'
import { getList as queryFreightTemplateAreaPage, queryForList as queryFreightTemplateArea, batchSave as batchSaveFreightTemplateArea } from '@/api/FreightTemplateArea/'
import { getList as queryLogistics, queryForList as queryLogisticsList } from '@/api/Logistics/'

import { getList as getOrganPager, querySubordinateOrgan, queryAssigneeOrgan } from '@/api/Organ'
import { queryAdministrativeRegion } from '@/api/areas'
import { getOrganId, getTenantId, getOrganPath, getCurrentLoginUserId, handleBuildTreeDataStruct, getSelectedNodes, getSelectedNodesObject, getSystemErrorCode, getCurrentTimeYYYY_MM_dd_HH_mm_ss } from '@/api/utils'
import { getTenantList, getOrganList, getDictionary } from '@/utils/auth'
import localStorageUtil from '@/utils/localStorageUtil'
import sessionStorageUtil from '@/utils/sessionStorageUtil'
export default {
  data() {
    return {
      list: null,
      listLoading: false,
      total: 5,
      currentPage: 1,
      pageSize: 10,
      dialogFormVisible: false,
      addForVisible: false,
      drawerFreightTemplateVisible: false,
      direction: 'rtl',
      freightTemplateItemList: [{ id: 1 }],
      administrativeRegionTree: [],
      logisticsList: [],
      dialogLogisticsListVisible: false,
      dialogFreightTemplateAreaListVisible: false,
      freightTemplateAreaList: [],
      form: {
        chargeType: '',
        createBy: '',
        createDate: '',
        featuers: '',
        fristNumPrice: '',
        fristVolumePrice: '',
        fristWeightPrice: '',
        id: '',
        isDeleted: '',
        logisticsCode: '',
        logisticsName: '',
        name: '',
        nextNumPrice: '',
        nextVolumePrice: '',
        nextWeightPrice: '',
        numMax: '',
        numMin: '',
        organId: '',
        organPath: '',
        parentId: '',
        remark: '',
        serviceType: '',
        sorted: '',
        status: '',
        tenantId: '',
        type: '',
        updateBy: '',
        updateDate: '',
        volumeMax: '',
        volumeMin: '',
        weightMax: '',
        weightMin: ''
      },
      freightTemplateArea: {
        areaId: '',
        cityId: '',
        createBy: '',
        createDate: '',
        features: '',
        id: '',
        isDeleted: '',
        organId: '',
        organPath: '',
        parentId: '',
        privinceId: '',
        remark: '',
        sorted: '',
        status: '',
        templateId: '',
        tenantId: '',
        updateBy: '',
        updateDate: ''
      },
      logistics: {
        id: ''
      },
      // 新增or编辑弹框标题(根据点击的新增or编辑按钮显示不同的标题)
      titleMap: {
        add: '新增',
        edit: '编辑'
      },
      FreightTemplateRules: {
      },
      props: { // 配置项（必选）
        value: 'id',
        label: 'organName',
        children: 'children',
        title: '组织',
        multiple: false,
        checkStrictly: true,
        emitPath: false
        // disabled:true
      },
      organTreeProps: {
        children: 'children',
        label: 'organName',
        url: 'url'
      },
      administrativeRegionProps: {
        children: 'children',
        label: 'name',
        url: 'url'
      },
      tenantProps: { // 配置项（必选）
        value: 'id',
        label: 'organName',
        children: 'children',
        title: '租户',
        multiple: false,
        checkStrictly: true,
        emitPath: false
        // disabled:true
      },
      organFormOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      tenantOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      organOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      supplierOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      organQueryModel: 'selfAndSub',
      supplier: null, //供应商
      goodsType: null, //商品分类
      // // 新增or编辑弹框标题(根据点击的新增or编辑按钮显示不同的标题)
      optionData: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      // 新增和编辑弹框标题
      dialogStatus: '',
      query: {
        params: {
          chargeType: '', //计量方式：1.按重量，2.按数量,
          createBy: '', //生成者,
          createDate: '', //生成日期,
          featuers: '', //拓展字段,
          fristNumPrice: '', //数量首费,
          fristVolumePrice: '', //体积首费,
          fristWeightPrice: '', //重要首费,
          id: '', //编号,
          isDeleted: '', //删除标识,
          logisticsCode: '', //物流商编号,
          logisticsName: '', //物流商名称,
          name: '', //运费模板名称,
          nextNumPrice: '', //数量续费,
          nextVolumePrice: '', //体积续费,
          nextWeightPrice: '', //重要续费,
          numMax: '', //最多数量,
          numMin: '', //最少数量,
          organId: '', //组织构构,
          organPath: '', //组织路径,
          parentId: '', //父节点编号,
          remark: '', //备注,
          serviceType: '', //服务类型：1:经济，2快速,
          sorted: '', //排序,
          status: '', //状态,
          tenantId: '', //所属租户,
          type: '', //模板类型，1.默认模板，2.自定义模板,
          updateBy: '', //更新者,
          updateDate: '', //更新日期,
          volumeMax: '', //最大体积,
          volumeMin: '', //最小体积,
          weightMax: '', //最大重量,
          weightMin: '', //最小重量,

        },
        freightTemplateArea: {
          areaId: '',
          cityId: '',
          createBy: '',
          createDate: '',
          features: '',
          id: '',
          isDeleted: '',
          organId: '',
          organPath: '',
          parentId: '',
          privinceId: '',
          remark: '',
          sorted: '',
          status: '',
          templateId: '',
          tenantId: '',
          updateBy: '',
          updateDate: '',
          total: 5,
          currentPage: 1,
          pageSize: 10,
        },
        logistics: {
          code: '',
          createBy: '',
          createDate: '',
          customFeeLimit: '',
          customTimeLimit: '',
          features: '',
          freightLimit: '',
          icon: '',
          id: '',
          isDeleted: '',
          name: '',
          organId: '',
          organPath: '',
          parentId: '',
          remark: '',
          show: '',
          sorted: '',
          status: '',
          tenantId: '',
          updateBy: '',
          updateDate: '',
          url: '',
          total: 5,
          currentPage: 1,
          pageSize: 10,
        },
        chargeTypeOptions: [],
        serviceTypeOptions: [],
        timeValue: '',
        options: [{
          value: '1',
          label: '黄金糕'
        }, {
          value: '2',
          label: '双皮奶'
        }],
        statusOptions:[],
        value: ''
      }
    }
  },
  created() {
    this.initOrganTree(() => {
      this.setOrganQueryType()
    }, getOrganPath())
    this.handleQueryAdministrativeRegion()
    this.initOrganFormTree()
    this.handleFiltTenant()
    this.handleBuildChargeTypeOptions()
    this.handleBuildServiceTypeOptions()
    this.handleBuildStatusOptions()
    this.fetchData()
  },
  methods: {
    createOrganQuery() {
      // 选中了租户
      let tenantSelected = this.$refs['tenantTree'] && this.$refs['tenantTree'].getCheckedNodes() || []
      let organSelected = this.$refs['organTree'] && this.$refs['organTree'].getCheckedNodes() || []
      let that = this
      this.query.params.organId = null
      if (tenantSelected.length && tenantSelected[0]) {
        this.query.params.tenantId = tenantSelected[0].data.id
        if (organSelected.length && organSelected[0]) {
          if (this.props.multiple) {
            this.query.params.organId = null
            this.query.params.organIds = []
            if (organSelected.length && organSelected[0]) {
              organSelected.forEach(function (element) {
                that.query.params.organIds.push(element.data.id)
              })
            }
          } else if (organSelected.length && organSelected[0]) {
            this.query.params.organPath = organSelected[0] && organSelected[0].data.organPath
          }
        } else {
          this.query.params.organId = getOrganId()
        }
      } else {
        this.query.params.tenantId = getTenantId()
        if (organSelected.length && organSelected[0]) {
          if (this.props.multiple) {
            this.query.params.organId = null
            this.query.params.organIds = []
            if (organSelected.length && organSelected[0]) {
              organSelected.forEach(function (element) {
                that.query.params.organIds.push(element.data.id)
              })
            }
          } else if (organSelected.length && organSelected[0]) {
            this.query.params.organPath = organSelected[0].data.organPath
          }
        } else {
          this.query.params.organId = getOrganId()
        }
        this.query.params.organPath = this.query.params.organPath || getOrganPath()

      }
    },
    fetchData() {
      this.listLoading = true
      this.createOrganQuery()
      getList(this.query.params, this.currentPage, this.pageSize).then(response => {
        const data = response.data
        if (!data.entities || typeof data.successful != 'undefined') {
          this.$message({
            type: 'info',
            message: data.description || getSystemErrorCode(data.status)
          })
        }
        this.total = data.totalCount
        this.pageSize = data.pageSize
        this.currentPage = data.currentPage
        this.list = data.entities
        this.listLoading = false
      })
    },

    handleSizeChange(val) {
      this.currentPage = 1
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    // 新增
    handleAdd() {
      // 显示弹框
      this.drawerFreightTemplateVisible = true
      // 新增弹框标题
      this.dialogStatus = 'add'

      this.form.chargeType = '1'
      this.form.createBy = getCurrentLoginUserId()
      this.form.createDate = getCurrentTimeYYYY_MM_dd_HH_mm_ss()
      this.form.featuers = '1'
      this.form.fristNumPrice = '1'
      this.form.fristVolumePrice = '1'
      this.form.fristWeightPrice = '1'
      this.form.id = ''
      this.form.isDeleted = ''
      this.form.logisticsCode = ''
      this.form.logisticsName = ''
      this.form.name = ''
      this.form.nextNumPrice = '1'
      this.form.nextVolumePrice = '1'
      this.form.nextWeightPrice = '1'
      this.form.numMax = '1'
      this.form.numMin = '1'
      this.form.organId = getOrganId()
      this.form.organPath = getOrganPath()
      this.form.parentId = '1'
      this.form.remark = '1'
      this.form.serviceType = ''
      this.form.sorted = '1'
      this.form.status = '1'
      this.form.tenantId = getTenantId()
      this.form.type = '1'
      this.form.updateBy = ''
      this.form.updateDate = ''
      this.form.volumeMax = '1'
      this.form.volumeMin = '1'
      this.form.weightMax = '1'
      this.form.weightMin = '1'
      this.addForVisible = false
    },
    // 编辑
    handleEdit(index, row) {
      console.log(index)
      console.log(row)
      this.form.chargeType = row.chargeType
      this.form.createBy = row.createBy
      this.form.createDate = row.createDate
      this.form.featuers = row.featuers
      this.form.fristNumPrice = row.fristNumPrice
      this.form.fristVolumePrice = row.fristVolumePrice
      this.form.fristWeightPrice = row.fristWeightPrice
      this.form.id = row.id
      this.form.isDeleted = row.isDeleted
      this.form.logisticsCode = row.logisticsCode
      this.form.logisticsName = row.logisticsName
      this.form.name = row.name
      this.form.nextNumPrice = row.nextNumPrice
      this.form.nextVolumePrice = row.nextVolumePrice
      this.form.nextWeightPrice = row.nextWeightPrice
      this.form.numMax = row.numMax
      this.form.numMin = row.numMin
      this.form.organId = row.organId
      this.form.organPath = row.organPath
      this.form.parentId = row.parentId
      this.form.remark = row.remark
      this.form.serviceType = row.serviceType
      this.form.sorted = row.sorted
      this.form.status = row.status
      this.form.tenantId = row.tenantId
      this.form.type = row.type
      this.form.updateBy = row.updateBy
      this.form.updateDate = row.updateDate
      this.form.volumeMax = row.volumeMax
      this.form.volumeMin = row.volumeMin
      this.form.weightMax = row.weightMax
      this.form.weightMin = row.weightMin
      this.addForVisible = true
      // 显示弹框
      this.dialogFormVisible = true
      // 编辑弹框标题
      this.dialogStatus = 'edit'
    },
    handleDelete(index, row) {
      console.log(index)
      console.log(row)

      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        doRemove(row).then(response => {
          const data = response.data
          console.log(data)
          if (data.successful) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.fetchData();
          } else {
            this.$message({
              type: 'info',
              message: data.description || getSystemErrorCode(data.status)
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleEditSave() {
      debugger
      doEdit(this.form).then(response => {
        const data = response.data
        if (data.successful) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData();
        } else {
          this.$message({
            type: 'info',
            message: '操作失败!'
          })
        }
        this.listLoading = false
        this.dialogFormVisible = false
      })
    },
    handleReset() {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    initOrganTree(fn, filter, that) {
      this.listLoading = true
      let data = getOrganList()
      if (filter) {
        data = data.filter(function (item, index) {
          return item.organPath.indexOf(filter) >= 0
        })
      }
      if (that && that.organFormOptions && that.organFormOptions.length >= 0) {
        that.organFormOptions = handleBuildTreeDataStruct(data, 'id', 'parentId')
      } else {
        this.organOptions = handleBuildTreeDataStruct(data, 'id', 'parentId')
      }
      this.listLoading = false
      if (fn) {
        fn()
      }
    },
    initOrganFormTree(fn, filter) {
      this.initOrganTree(fn, filter, this)
    },
    setOrganQueryType() {
      this.organQueryModelChange(localStorageUtil.fetch("SYSTEM_SETTING_ORGAN_QUERY_MODEL"))
    },
    selectOrgan(nodes) {
      let nodesInfo = this.$refs['organTree'].getCheckedNodes()
      let that = this
      this.query.params.organPath = ''
      this.query.params.organIds = []
      if (this.props.multiple) {
        this.query.params.organIds = []
        if (nodesInfo.length) {
          nodesInfo.forEach(function (element) {
            that.query.params.organIds.push(element.data.id)
          })
        }
      } else if (nodesInfo.length) {
        this.query.params.organPath = nodesInfo[0].data.organPath
      }
    },
    selectTenant(nodes) {
      debugger
      let nodesInfo = this.$refs['tenantTree'].getCheckedNodes()
      let that = this
      this.query.params.tenantId = ''
      this.query.params.tenantIds = []
      this.query.params.organId = null
      this.query.params.organIds = []
      this.query.params.organPath = null

      if (this.tenantProps.multiple) {
        this.query.params.tenantIds = []
        if (nodesInfo.length) {
          nodesInfo.forEach(function (element) {
            that.query.params.tenantIds.push(element.data.id)
          })
        }
      } else if (nodesInfo.length) {
        this.query.params.tenantId = nodesInfo[0].data.id
        this.initOrganTree(function () {
          that.setOrganQueryType()
        }, nodesInfo[0].data.organPath)
      } else {
        this.initOrganTree(function () {
          that.setOrganQueryType()
        })
      }
    },
    chooseTenantForm(nodes) {
      let nodesInfo = this.$refs['tenantTreeForm'].getCheckedNodes()
      this.form.tenantId = null
      let that = this
      debugger
      if (nodesInfo.length) {
        this.form.tenantId = nodesInfo[0].data.id
        this.initOrganFormTree(function () {
          that.setOrganQueryType()
        }, nodesInfo[0].data.organPath, this.organFormOptions)
      } else {
        this.initOrganFormTree(function () {
          that.setOrganQueryType()
        }, null, this.organFormOptions)
      }
    },
    organQueryModelChange(organQueryModel) {
      if (organQueryModel == 'selected') {
        this.props.multiple = true
      } else if (organQueryModel == 'selfAndSub') {
        this.props.multiple = false
      }
    },
    chooseOrganForm(nodes) {
      let nodesInfo = this.$refs['organTreeForm'].getCheckedNodes()
      if (nodesInfo[0]) {
        this.form.organPath = nodesInfo[0].data.organPath
        this.form.organId = nodesInfo[0].data.id
        this.form.tenantId = nodesInfo[0].data.tenantId
      }
    },
    handleFiltTenant() {
      let data = getOrganList()
      this.tenantOptions = data.filter(function (item, index) {
        return item.type == 0 || item.type == 1;
      })
    },
    handleBuildChargeTypeOptions() {
      const datas = getDictionary()
      let data = datas.filter(function (item, index) {
        return item.nameSpace === "MINISOTRE.FREIGHT_TEMPLATE.CHARGE_TYPE" && item.parentId != '0';
      })
      let chargeTypeOptions = [];
      for (let i = 0; i < data.length; i++) {
        let option = {}
        option['value'] = data[i].value
        option['label'] = data[i].desc
        chargeTypeOptions.push(option)
      }
      this.query.chargeTypeOptions = chargeTypeOptions
    },
    handleBuildServiceTypeOptions() {
      const datas = getDictionary()
      let data = datas.filter(function (item, index) {
        return item.nameSpace === "MINISOTRE.FREIGHT_TEMPLATE.SERVICE_TYPE" && item.parentId != '0';
      })
      let serviceTypeOptions = [];
      for (let i = 0; i < data.length; i++) {
        let option = {}
        option['value'] = data[i].value
        option['label'] = data[i].desc
        serviceTypeOptions.push(option)
      }
      this.query.serviceTypeOptions = serviceTypeOptions
    },

    handleQueryAdministrativeRegion() {
      let that = this
      queryAdministrativeRegion().then(response => {
        const data = response.data
        if (data.successful) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          that.administrativeRegionTree = handleBuildTreeDataStruct(data.data, 'id', 'parentId')
          console.log(that.administrativeRegionTree)
        } else {
          this.$message({
            type: 'info',
            message: '操作失败!'
          })
        }
      })
    },
    handleCreateFreightTemplate() {
      let checkNodes = this.$refs.regionTree.getCheckedNodes()

      doEdit(this.form).then(response => {
        const data = response.data
        if (data.successful) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          let freightTemplateAreas = []
          for (let i = 0; i < checkNodes.length; i++) {
            let freightTemplateArea = {
              areaId: checkNodes[i].id,
              cityId: checkNodes[i].parentId,
              area: checkNodes[i].name,
              status: '1',
            }
            freightTemplateAreas.push(freightTemplateArea)
          }
          batchSaveFreightTemplateArea({
            entities: freightTemplateAreas,
            createBy: getCurrentLoginUserId(),
            createDate: getCurrentTimeYYYY_MM_dd_HH_mm_ss(),
            id: data.data.id,
            organPath: this.form.organPath || getOrganPath,
            organId: this.form.organId || getOrganId(),
            tenantId: this.form.tenantId || getTenantId()
          }).then(response => {
            const gateway = response.data
            if (gateway.successful) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
            } else {
              this.$message({
                type: 'info',
                message: gateway.description || getSystemErrorCode(gateway.status)
              })
            }
          })

          this.fetchData();
        } else {
          this.$message({
            type: 'info',
            message: data.description || getSystemErrorCode(data.status)
          })
        }
        this.listLoading = false
      })
    },
    handleShowFreightTemplate(row) {
      this.dialogFreightTemplateAreaListVisible = true
      this.query.freightTemplateArea.templateId = row.id
      this.query.freightTemplateArea.organId = row.organId || getOrganId()
      this.query.freightTemplateArea.organPath = row.organPath || getOrganPath()
      this.query.freightTemplateArea.tenantId = row.tenantId || getTenantId()
      this.handleQueryFreightTemplate()

    },
    handleQueryFreightTemplate() {
      let that = this
      that.query.freightTemplateArea.currentPage = that.query.freightTemplateArea.currentPage || 1
      that.query.freightTemplateArea.pageSize = that.query.freightTemplateArea.pageSize || 10
      queryFreightTemplateAreaPage(that.query.freightTemplateArea,
        that.query.freightTemplateArea.currentPage,
        that.query.freightTemplateArea.pageSize).then(response => {
          let data = response.data
          that.query.freightTemplateArea.total = data.totalCount
          that.query.freightTemplateArea.pageSize = data.pageSize
          that.query.freightTemplateArea.currentPage = data.currentPage
          that.freightTemplateAreaList = data.entities
        })
    },

    handleFreightTemplateAreaSizeChange(val) {
      this.query.freightTemplateArea.pageSize = val
      this.query.freightTemplateArea.currentPage = 1
      this.handleQueryFreightTemplate()
    },
    handleFreightTemplateAreaCurrentChange(val) {
      this.query.freightTemplateArea.currentPage = val
      this.handleQueryFreightTemplate()
    },
    handleShowLogistics() {
      this.dialogLogisticsListVisible = true
      this.query.logistics.tenantId = this.query.logistics.tenantId || getTenantId()
      this.query.logistics.organId = this.query.logistics.organId || getOrganId()
      this.query.logistics.organPath = this.query.logistics.organPath || getOrganPath()
      queryLogisticsList(this.query.logistics).then(response => {
        const gateway = response.data
        if (gateway.successful) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.logisticsList = gateway.data
        } else {
          this.$message({
            type: 'info',
            message: gateway.description || getSystemErrorCode(gateway.status)
          })
        }

      })
    },
    handelChooseLogistics(logistics) {
      this.form.logisticsCode = logistics.code
      this.form.logisticsName = logistics.name
      if (logistics.customFeeLimit) {
        this.form.isFeeLimit = '1'
      } else {
        this.form.isFeeLimit = '0'
      }

    },
    handleClose(done) {
      this.$confirm('确认关闭新增运费模板吗？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    handleBuildStatusOptions() {
      const datas = getDictionary()
      let data = datas.filter(function(item, index) {
        return item.bigType === "BizEntity" && item.parentId != '0';
      })
      let statusOptions = [];
      for (let i = 0; i < data.length; i++) {

        let option = {}
        option['value'] = data[i].value
        option['label'] = data[i].desc
        statusOptions.push(option)
      }
      this.query.statusOptions = statusOptions
    },


  }
}
</script>
